﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.Public;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class CrossConnAlignment : ConnAlignment
	{
		public CrossConnAlignment(Document doc) : base(doc)
		{
		}

		public override bool connectAlignment(List<Duct> ducts, List<XYZ> points)
		{
			bool result = false;
			SubTransaction subTransaction = null;
			try
			{
				if (!this.MeetFittingCondition(ducts, points))
				{
					return false;
				}
				List<Duct> list = ducts;
				List<Alignments> list2;
				if (ducts[0].UniqueId == ducts[1].UniqueId || ducts[2].UniqueId == ducts[3].UniqueId)
				{
					list2 = this.GetAlignmentBeforeSperate(ducts);
					list = this.GetNewdDucts(ducts, points);
					if (list == null || list2 == null)
					{
						return false;
					}
				}
				else
				{
					list2 = this.GetAlignment(list);
				}
				if (list2 == null || list2.Count != 2)
				{
					base.ErrorString = "风管没有正确的对齐方式";
					return false;
				}
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1]);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2]);
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(list[3]);
				Connector closeConnector = hypiping.GetCloseConnector(hypiping3.StartConnector.Origin, false);
				Connector closeConnector2 = hypiping3.GetCloseConnector(hypiping.StartConnector.Origin, false);
				Connector closeConnector3 = hypiping2.GetCloseConnector(hypiping.StartConnector.Origin, false);
				Connector closeConnector4 = hypiping4.GetCloseConnector(hypiping.StartConnector.Origin, false);
				if (closeConnector.IsConnected || closeConnector3.IsConnected || closeConnector2.IsConnected || closeConnector4.IsConnected)
				{
					YJKMessageBox.Show("选择的风管已经被连接，请重新选择风管");
					return false;
				}
				NineAlignments oneAlignInNineAlignments = base.GetOneAlignInNineAlignments(list2);
				string familyName = "";
				this.GetFamilyName(list, oneAlignInNineAlignments, ref familyName);
				FamilyInstance familyInstance = base.GetFamilyInstance(base.Doc, list, familyName, false);
				if (familyInstance == null)
				{
					base.ErrorString = "没有找到相应的族，连接失败";
					return false;
				}
				base.RotateElementToAlignAllDucts(familyInstance, closeConnector, closeConnector3, closeConnector2, base.Doc);
				subTransaction = new SubTransaction(base.Doc);
				subTransaction.Start();
				List<Connector> listConn = new List<Connector>
				{
					closeConnector,
					closeConnector3,
					closeConnector2,
					closeConnector4
				};
				base.ModifySizeAndConnect(listConn, familyInstance);
				subTransaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				base.ErrorString = ex.ToString();
				return false;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
                    if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public override bool MeetFittingCondition(List<Duct> ducts, List<XYZ> points)
		{
			if (ducts.Count != 4 || points.Count != 4)
			{
				base.ErrorString = "四通连接必须是四条风管！";
				return false;
			}
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], points[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], points[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], points[2]);
			YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3], points[3]);
			if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id) && hypiping.Piping.Id.Equals(hypiping4.Piping.Id))
			{
				base.ErrorString = "同一根风管不能构成四通连接！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				base.ErrorString = "第一根风管与第三根风管不能为同一风管！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping4.Piping.Id))
			{
				base.ErrorString = "第一根风管与第四根风管不能为同一风管！";
				return false;
			}
			if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				base.ErrorString = "第二根风管与第三根风管不能为同一风管！";
				return false;
			}
			if (hypiping2.Piping.Id.Equals(hypiping4.Piping.Id))
			{
				base.ErrorString = "第二根风管与第四根风管不能为同一风管！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[2]))
			{
				base.ErrorString = "第一根风管与第三根风管必须垂直！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[3]))
			{
				base.ErrorString = "第一根风管与第四根风管必须垂直！";
				return false;
			}
			if (!hypiping.IsParallel(ducts[1]))
			{
				base.ErrorString = "第一根风管与第二根风管必须平行！";
				return false;
			}
			if (!hypiping3.IsParallel(ducts[3]))
			{
				base.ErrorString = "第三根风管与第四根风管必须平行！";
				return false;
			}
			return true;
		}

		public override List<Alignments> GetAlignmentBeforeSperate(List<Duct> ducts)
		{
			if (ducts[0].UniqueId == ducts[1].UniqueId && ducts[2].UniqueId == ducts[3].UniqueId)
			{
				List<Duct> ducts2 = new List<Duct>
				{
					ducts[0],
					ducts[1],
					ducts[3]
				};
				return base.GetAlignmentTeeBeforeSperate(ducts2);
			}
			if (ducts[0].UniqueId == ducts[1].UniqueId && ducts[2].UniqueId != ducts[3].UniqueId)
			{
				List<Duct> ducts3 = new List<Duct>
				{
					ducts[0],
					ducts[2],
					ducts[3]
				};
				return base.GetAlignmentByYShapeTee(ducts3);
			}
			if (ducts[0].UniqueId != ducts[1].UniqueId && ducts[2].UniqueId == ducts[3].UniqueId)
			{
				List<Duct> ducts4 = new List<Duct>
				{
					ducts[0],
					ducts[1],
					ducts[3]
				};
				return base.GetAlignmentByRoundAngleTee(ducts4);
			}
			return null;
		}

		public override List<Duct> GetNewdDucts(List<Duct> ducts, List<XYZ> points)
		{
			if (ducts[0].UniqueId == ducts[1].UniqueId && ducts[2].UniqueId == ducts[3].UniqueId)
			{
				List<Duct> ductsBySperateDuct = base.GetDuctsBySperateDuct(ducts[0], ducts[3], points[0]);
				if (ductsBySperateDuct == null)
				{
					return null;
				}
				List<Duct> ductsBySperateDuct2 = base.GetDuctsBySperateDuct(ducts[3], ductsBySperateDuct[0], points[2]);
				if (ductsBySperateDuct2 == null)
				{
					return null;
				}
				return new List<Duct>
				{
					ductsBySperateDuct[0],
					ductsBySperateDuct[1],
					ductsBySperateDuct2[0],
					ductsBySperateDuct2[1]
				};
			}
			else if (ducts[0].UniqueId == ducts[1].UniqueId && ducts[2].UniqueId != ducts[3].UniqueId)
			{
				List<Duct> ductsBySperateDuct3 = base.GetDuctsBySperateDuct(ducts[0], ducts[3], points[0]);
				if (ductsBySperateDuct3 == null)
				{
					return null;
				}
				return new List<Duct>
				{
					ductsBySperateDuct3[0],
					ductsBySperateDuct3[1],
					ducts[2],
					ducts[3]
				};
			}
			else
			{
				if (!(ducts[0].UniqueId != ducts[1].UniqueId) || !(ducts[2].UniqueId == ducts[3].UniqueId))
				{
					return null;
				}
				List<Duct> ductsBySperateDuct4 = base.GetDuctsBySperateDuct(ducts[3], ducts[0], points[2]);
				if (ductsBySperateDuct4 == null)
				{
					return null;
				}
				return new List<Duct>
				{
					ducts[0],
					ducts[1],
					ductsBySperateDuct4[0],
					ductsBySperateDuct4[1]
				};
			}
		}

		public override List<Alignments> GetAlignment(List<Duct> ducts)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
			Connector closeConnector = hypiping.GetCloseConnector(hypiping2.StartConnector.Origin, false);
			DetermineTwoDuctsAlignment determineTwoDuctsAlignment = new DetermineTwoDuctsAlignment(closeConnector.CoordinateSystem.BasisZ, ducts[0], ducts[1]);
			List<Alignments> verticalAlignmentsByTwoDucts = determineTwoDuctsAlignment.GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts.Count <= 0)
			{
				base.ErrorString = "第一根管和第二根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> verticalAlignmentsByTwoDucts2 = new DetermineTwoDuctsAlignment(closeConnector.CoordinateSystem.BasisZ, ducts[0], ducts[2]).GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts2.Count <= 0)
			{
				base.ErrorString = "第一根管和第三根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> list = verticalAlignmentsByTwoDucts.Intersect(verticalAlignmentsByTwoDucts2).ToList<Alignments>();
			if (list.Count <= 0)
			{
				base.ErrorString = "四根管没有共同的对齐方式";
				return null;
			}
			DetermineTwoDuctsAlignment determineTwoDuctsAlignment2 = new DetermineTwoDuctsAlignment(-closeConnector.CoordinateSystem.BasisX, ducts[2], ducts[3]);
			List<Alignments> verticalAlignmentsByTwoDucts3 = determineTwoDuctsAlignment2.GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts3.Count <= 0)
			{
				base.ErrorString = "第三根管和第四根管没有符合的对齐方式";
				return null;
			}
			list = list.Intersect(verticalAlignmentsByTwoDucts3).ToList<Alignments>();
			if (list.Count <= 0)
			{
				base.ErrorString = "四根管没有共同的对齐方式";
				return null;
			}
			if (list.Count > 1)
			{
				list.Clear();
				list.Add(Alignments.VerticalMiddle);
			}
			List<Alignments> list2 = determineTwoDuctsAlignment.GetHorizontalAlignmentsByTwoDucts();
			if (list2.Count <= 0)
			{
				base.ErrorString = "第一根管和第二根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> horizontalAlignmentsByTwoDucts = determineTwoDuctsAlignment2.GetHorizontalAlignmentsByTwoDucts();
			if (horizontalAlignmentsByTwoDucts.Count <= 0)
			{
				base.ErrorString = "第三根管和第四根管没有符合的对齐方式";
				return null;
			}
			list2 = list2.Intersect(horizontalAlignmentsByTwoDucts).ToList<Alignments>();
			if (list2.Count <= 0)
			{
				base.ErrorString = "四根管没有符合的对齐方式";
				return null;
			}
			if (list2.Count > 1)
			{
				list2.Clear();
				list2.Add(Alignments.HorizontalMiddle);
			}
			return list.Union(list2).ToList<Alignments>();
		}

		public override bool GetFamilyName(List<Duct> ducts, NineAlignments na, ref string familyName)
		{
			List<string> nineFamilyNames = this.GetNineFamilyNames();
			switch (na)
			{
			case NineAlignments.TopLeft:
				familyName = nineFamilyNames[0];
				break;
			case NineAlignments.TopRight:
				familyName = nineFamilyNames[1];
				break;
			case NineAlignments.TopMiddle:
				familyName = nineFamilyNames[2];
				break;
			case NineAlignments.BottomLeft:
				familyName = nineFamilyNames[3];
				break;
			case NineAlignments.BottomRight:
				familyName = nineFamilyNames[4];
				break;
			case NineAlignments.BottomMiddle:
				familyName = nineFamilyNames[5];
				break;
			case NineAlignments.MiddleLeft:
				familyName = nineFamilyNames[6];
				break;
			case NineAlignments.MiddleRight:
				familyName = nineFamilyNames[7];
				break;
			case NineAlignments.MiddleMiddle:
				familyName = nineFamilyNames[8];
				break;
			}
			return false;
		}
	}
}
